package j.framework.tags.easyui;

import j.framework.tags.form.FormTagWriter;
import j.util.DateUtils;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;
import java.util.Date;
import java.util.Map;

/**
 * 日期控件
 * Created by junhai on 2015/1/17.
 */
public class DateTag extends BaseEasyuiFormControl {
    protected Date date;//当前日期，子类DatetimeBox继承此类时，该值为日期时间类型
    private String onSelect;//当用户选择一个日期时触发。方法签名：date
    private Boolean editable;

    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("input");
    }

    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {
        Date dateValue = date;
        if(dateValue == null){
            Object value = getFormDataValue();
            if(value!=null && value instanceof Date){
                dateValue = (Date)value;
            }
        }
        if(dateValue!=null){
            String dateFormat = "yyyy-MM-dd";
            if(this instanceof DateTimeTag){
                dateFormat+=" HH:mm:ss";
            }
            writer.writeFormTagProperty("value", DateUtils.format(dateValue, dateFormat));
        }
    }

    @Override
    public void writeEasyuiDataOptions(Map<String, Object> dataOptions) throws JspException {
        if(StringUtils.isNotEmpty(onSelect)){
            dataOptions.put("onSelect",onSelect);
        }
        if(editable!=null){
            dataOptions.put("editable",editable);
        }else{
            dataOptions.put("editable",false);
        }
    }

    @Override
    public void endBeginTag(FormTagWriter writer) throws JspException {
        writer.closeBlankTag();
    }

    @Override
    public String getEasyuiClassName() {
        return "easyui-datebox";
    }

    @Override
    protected String getControlWidth() {
        return "150px";
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setOnSelect(String onSelect) {
        this.onSelect = onSelect;
    }

    public void setEditable(Boolean editable) {
        this.editable = editable;
    }
}
